package com.barunsw.bwf.ems.tool.alarm_generator;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.stereotype.Component;

import com.barunsw.bwf.ems.common.model.AlarmMessage;
import com.barunsw.bwf.ems.util.TimeUtil;

@Component
public class AlarmGenerator {
    private static final Logger LOGGER = LoggerFactory.getLogger(AlarmGenerator.class);
    
    @Autowired
    private SimpMessagingTemplate template;
    
    private TaskScheduler scheduler = new ConcurrentTaskScheduler();
    
	@PostConstruct
    public void run() {
		LOGGER.debug("AlarmGenerator run");
		
		scheduler.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				//generate_A1000();
			}
		}, 10000);
    }
	
    private void sendAlarmMessage(AlarmMessage alarmMessage) {
		try {
			LOGGER.debug("sendAlarmMessage:" + alarmMessage);
			
			template.convertAndSend("/topic/alarmmessagesresponse", alarmMessage);
		}
		catch ( Exception ex ) {
			LOGGER.error(ex.getMessage(), ex);
		}
	}
    
	public void generate_A1000() {
		String filePath =  "/com/barunsw/bwf/ems/neoarena/tool/alarm_generator/data/A1000_NetworkDown.dat";
		
		List<AlarmMessage> alarmList = new ArrayList<AlarmMessage>();
		
		BufferedReader bufferedReader = null;
		try {
			InputStream inputStream = this.getClass().getResourceAsStream(filePath);
			
			LOGGER.debug("inputStream:" + inputStream);
			
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
			String readLine = null;
			while ( (readLine = bufferedReader.readLine()) != null ) {
				readLine = readLine.trim();
				
				if ( readLine.startsWith("#") || readLine.length() == 0 ) {
					continue;
				}
				else {
					String[] alarmTokens = readLine.split(",");
					
					String sev 		= alarmTokens[0];
					String alarmId 	= alarmTokens[1];
					String loc		= alarmTokens[2];
					String alarmMsg = alarmTokens[3];
					
					AlarmMessage alarmMessage = new AlarmMessage();
					alarmMessage.setSev(sev);
					alarmMessage.setAlarmId(alarmId);
					alarmMessage.setLoc(loc);
					alarmMessage.setAlarmMsg(alarmMsg);
					alarmMessage.setEventTime(TimeUtil.getCurrentTimeStr());
					
					alarmList.add(alarmMessage);
				}
			}
		}
		catch ( Exception ex ) {
			LOGGER.error(ex.getMessage(), ex);
		}
		finally {
			if ( bufferedReader != null ) {
				try {
					bufferedReader.close();
				}
				catch ( Exception ex ) {
					LOGGER.error(ex.getMessage(), ex);
				}
			}
		}
		
		for ( AlarmMessage oneAlarmMessage : alarmList ) {
			sendAlarmMessage(oneAlarmMessage);
		}
	}
}
